package com.policenacorp.ejb.impl;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@MessageDriven(activationConfig = {
	@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
	@ActivationConfigProperty(propertyName = "destination", propertyValue = "queues/queueActive"),
	@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
public class MessageDrivenBusiness implements MessageListener {

    @Resource
    private MessageDrivenContext context;

    // @Resource(name = "jms/connectionFactory")
    // private ConnectionFactory connectionFactory;

    // @Resource(name = "queues/queueActive")
    // private Queue destinationQueue;

    @Override
    public void onMessage(Message message) {
	try {
	    StringBuilder sBuilder = new StringBuilder();
	    sBuilder.append("Chegou mensagem:\n");
	    final TextMessage textMessage = (TextMessage) message;
	    sBuilder.append(textMessage.getText());
	    sBuilder.append(String.format("\nCaller: %s", context.getCallerPrincipal().getName()));
	    System.out.println(sBuilder);
	} catch (JMSException e) {
	    e.printStackTrace();
	}
    }

}
